if(!window.KD) KD={};

KDWin={};

KDWin.openDialog=function(url_,options_){
	  var xW=options_.X?options_.X:800;
	  var yH=options_.Y?options_.Y:600;
	  
		var x0 = parseInt(screen.width / 2.0) - (xW / 2.0);
		var y0 = parseInt(screen.height / 2.0) - (yH / 2.0);
    if (document.all) {// only ie have it
			retval = window
					.showModalDialog(
							url_,
							window,
							"dialogWidth:"
									+ xW
									+ "px; dialogHeight:"
									+ yH
									+ "px; dialogLeft:"
									+ x0
									+ "px; dialogTop:"
									+ y0
									+ "px; status:no; directories:yes;scrollbars:no;Resizable=yes;");
		} else {
			var win = window.open(url_, "daglog", "top=" + y0 + ",left=" + x0
					+ ",width=" + xW + ",height="
					+ yH+",toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, status=yes");
			eval('try { win.resizeTo(width, height); } catch(e) { }');
			win.focus();
		}
}

KDWin.open=function(url_,options_){
	   var xW=options_.X?options_.X:800;
	   var yH=options_.Y?options_.Y:600;
	   var roll=options_.scroll?options_.scroll:'no';
		 var x0 = parseInt(screen.width / 2.0) - (xW / 2.0);
		 var y0 = parseInt(screen.height / 2.0) - (yH / 2.0);
		 //add resizable setting
		 var resizable=options_.resizable;
		 if(resizable==null||resizable==undefined){
		 	resizable="yes";
		 }
		 var win = window.open(url_, options_.name, "top=" + y0 + ",left=" + x0
					+ ",width=" + xW + ",height="+
				  yH+", scrollbars="+roll+",toolbar=no, menubar=no, resizable="+resizable+", location=no, status=yes,depended=yes,z-look=true");
		 eval('try { win.resizeTo(width, height); } catch(e) { }');
		 win.focus();
}


KDWin.resizeGridWidth=function(pcDivId,gridjsVar){
	var gridObj=eval(gridjsVar);
	var divObj=document.getElementById(pcDivId);
	var screenWidth=document.documentElement.clientWidth;
	if(screenWidth==0){
	    	screenWidth=document.body.clientWidth;
	}	
	if(divObj!=null){
		var tdclientWidth=screenWidth;//divObj.clientWidth;
		divObj.width=tdclientWidth;
		divObj.style.width=tdclientWidth;
		gridObj.setWidth(tdclientWidth);
		var tdclientHeight=divObj.clientHeight;
		gridObj.setHeight(tdclientHeight);
		gridObj.syncSize();
		gridObj.setSize(tdclientWidth,tdclientHeight);
	}    
}


function createXMLHttpRequest() {
	if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
}

function releaseLc() {
	try{
   		var xmlHttp = createXMLHttpRequest();
   		var className = document.getElementById("beanClassName").value;
   		var ajaxUrl = document.getElementById("lcAjaxUrl").value;
       	var reqUrl = ajaxUrl + "?method=release&beanClassName=" + className;
       	
       	if(className != null) {
       		xmlHttp.open("POST", reqUrl, false);
       		xmlHttp.send(null);
       		//var result = xmlHttp.responseText;
       	}
    }
    catch(e) {
       //alert(e.name + ": " + e.message);
    }
}

function releaseNetCtrl() {
	try{
   		var xmlHttp = createXMLHttpRequest();
   		var dataObjId = document.getElementById("dataObjId").value;
   		var ajaxUrl = document.getElementById("ncAjaxUrl").value;
       	var reqUrl = ajaxUrl + "?method=release&dataObjId=" + encodeURIComponent(dataObjId);
       	
       	if(dataObjId != null && dataObjId!="") {       		
       		xmlHttp.open("POST", reqUrl, false);
       		xmlHttp.send(null);
       		//var result = xmlHttp.responseText;
       	}
    }
    catch(e) {
       //alert(e.name + ": " + e.message);
    }
}
/*
function releaseWinletBean() {
	try{
   		var xmlHttp = createXMLHttpRequest();
   		var beanName = document.getElementById("beanName").value;
   		var ajaxUrl = document.getElementById("wbrAjaxUrl").value;
       	var reqUrl = ajaxUrl + "?method=release&beanName=" + beanName;
       	
       	if(beanName != null) {       		
       		xmlHttp.open("POST", reqUrl, false);
       		xmlHttp.send(null);
       		//var result = xmlHttp.responseText;
       	}
    }
    catch(e) {
       //alert(e.name + ": " + e.message);
    }
}
*/

window.onbeforeunload=function (){
	if(window.bizBeforeunload){
			bizBeforeunload();
	}
	wafBeforeunload();
	if(window.bizAfterunload){
		bizAfterunload();
	} 		
}

function wafBeforeunload() {
	releaseLc();
	releaseNetCtrl();
	//releaseWinletBean();
	
	//call back parent window js method.
	try
	{
		if (document.all) {// only ie have it
		if(window.dialogArguments != null && window.dialogArguments != "undefined" && 
				typeof(window.dialogArguments.afterDialogClosed) != "undefined") {
			window.dialogArguments.afterDialogClosed(window.location.href);
		} else if(window.opener != null && window.opener != "undefined" && 
					typeof(window.opener.afterDialogClosed) != "undefined") {
			window.opener.afterDialogClosed(window.location.href);
		}
		} else {
		//alert(window.opener.location.href);
		if(window.opener != null && window.opener != "undefined" &&
				typeof(window.opener.afterDialogClosed) != "undefined") {
			window.opener.afterDialogClosed(window.location.href);
		}
		}
	}  catch(e){}
}

function decodeAjaxResponse(resString) {
	var begin = resString.indexOf("@BEGIN_RET@");
	var end = resString.indexOf("@END_RET@");
	return resString.substring(begin + 11, end);
}

function ajaxFuncInvoke(source) {
     if (document.createEvent) {
	     var evObj = document.createEvent('MouseEvents');
	     evObj.initEvent( 'click', true, false );
	     source.dispatchEvent(evObj);
     } else if (document.createEventObject) {
     	 source.fireEvent('click');
     }
}

if(Ext.form.TextField)
{
	Ext.form.TextField.prototype.size = 20;
	Ext.form.TextField.prototype.initValue = function()
	{
	    if(this.value !== undefined){
	        this.setValue(this.value);
	    }else if(this.el.dom.value.length > 0){
	        this.setValue(this.el.dom.value);
	    }
	    this.el.dom.size = this.size;
	    if (!isNaN(this.maxLength) && (this.maxLength *1) > 0 && (this.maxLength != Number.MAX_VALUE)) {
	        this.el.dom.maxLength = this.maxLength *1;
	    }   
	};
}

KD.setElmAttr=function(elm,atts){

  for (att in atts){

    elm.setAttribute(att,atts[att]);

  }

}

var save_callback=null;
function saveAndCallBack(callback){
       if(window.beforeSave)beforeSave();
		save_callback=callback;
		if(typeof(btnCallBackSave)!='undefined')
		{
			if(!btnCallBackSave.disabled){
				btnCallBackSave.fireEvent("click"); 
				return true;
			}
			return false;
       	}else{
			return false;
        }
}




//function reLayout(_obj, _ctrl, _anchor, _offset, _size, _parent_size, _cur_parent_size, _subs){
function reLayout(){
  var _obj = arguments[0];
  var _ctrl = arguments[1];
  var _anchor = arguments[2];
  var _offset = arguments[3];
  var _size = arguments[4];
  var _parent_size = arguments[5];
  var _cur_parent_size = arguments[6];

  if("undefined" == typeof _obj)
    return;

	if(_anchor == 4){//left anchor
		_obj.setPosition(_offset[0],_offset[1]);
		_obj.setSize(_size[0],_size[1]);
		if(_ctrl != null)
			_ctrl.setSize(_size[0],_size[1]);
	}if(_anchor == 260){//left anchor right scale
	var width = _cur_parent_size[0] * _size[0] / _parent_size[0];
		_obj.setPosition(_offset[0],_offset[1]);
		_obj.setSize(width,_size[1]);
		if(_ctrl != null)
			_ctrl.setSize(width,_size[1]);
	}else if(_anchor == 8){//right anchor
		var x = _cur_parent_size[0] - (_parent_size[0] - _offset[0]);
		_obj.setPosition(x,_offset[1]);
		_obj.setSize(_size[0],_size[1]);
		if(_ctrl != null)
			_ctrl.setSize(_size[0],_size[1]);
	}else if(_anchor == 136){//right anchor left scale
		var space = _parent_size[0] - _offset[0] - _size[0];
		var width = _cur_parent_size[0] * _size[0] / _parent_size[0];
		var x = _cur_parent_size[0] - width - space;
		_obj.setPosition(x,_offset[1]);
		_obj.setSize(width,_size[1]);
		if(_ctrl != null)
			_ctrl.setSize(width,_size[1]);
	}else if(_anchor == 12){//left and right anchor
		var width = _cur_parent_size[0] - _parent_size[0] + _size[0] ;
		_obj.setPosition(_offset[0],_offset[1]);
		_obj.setSize(width,_size[1]);
		if(_ctrl != null)
			_ctrl.setSize(width,_size[1]);
	}else if(_anchor == 384){//left and right scale
		
	}else if(_anchor == 16){ // center anchor
		var x = _cur_parent_size[0] * (_size[0]/2 + _offset[0]) / _parent_size[0] - (_size[0]/2);
		_obj.setPosition(x,_offset[1]);
		_obj.setSize(_size[0],_size[1]);
	}
}